package com.p2p.front.dal.model;

import java.io.Serializable;
import java.util.Date;

import com.mysql.jdbc.TimeUtil;
import com.p2p.base.model.Money;
import com.p2p.base.util.DateUtil;

public class LoanVariation implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -900130448505746264L;
	
	private Long loanId;//贷款标id
	private Money amount = new Money();//贷款金额
	private Money remainAmount = new Money();//剩余本金
	private Money returnAmount = new Money();//已经还总额
	private Integer repayNum;//已还期数
	private Money tenderAmount = new Money();//已投标总额
	private Money flowAmount = new Money();//已流转金额
	private Money repoAmount = new Money();//已回购金额
	private Date flowDate;//流转处理日期
	private Integer status;//状态
	private Integer tenderCount;//总投标次数
	private Date tenderBegin;//开标时间
	private Date tenderEnd;//满标时间
	private Date expiredTime;//过期时间
	private Date updateTime;//系统更新时间
	private Money totalInterest = new Money(); //所有应偿还利息总额
	
	private Integer oldStatus;//旧状态
	
	public Money addTenderAmount(Money addMoney){
		
		Long canTender = this.amount.getCent()-this.tenderAmount.getCent();
		
		if(canTender<=0){
			return new Money();
		}
		if(addMoney.getCent()>canTender){
			addMoney.setCent(canTender);
		}
		this.tenderAmount.add(addMoney);
		
		return addMoney;
	}
	
	
	public Money subTenderAmount(Money subMoney){
		Long allreadyTender = this.tenderAmount.getCent();
		if(allreadyTender>=subMoney.getCent()){
			tenderAmount.sub(subMoney);
			return subMoney;
		}
		return new Money();
	}
	
	public Money addTenderAmount(Tender tender){
		
		Long canTender = this.amount.getCent()-this.tenderAmount.getCent();
		
		if(canTender<=0){
			return new Money();
		}
		if(tender.getAmount().getCent()>canTender){
			long temp = tender.getRepayAmount().getCent()*canTender/tender.getAmount().getCent();
			tender.getAmount().setCent(canTender);
			tender.getRepayAmount().setCent(temp);
		}
		this.tenderAmount.add(tender.getAmount());
		
		return tender.getAmount();
	}

	public Integer getFullTimeConsuming() throws Exception{
		if(this.tenderEnd!=null&&tenderBegin!=null){
			int secondes =DateUtil.getSecondsBetweenTwoDate(tenderEnd, tenderBegin);
			return secondes;
		}else{
			return null;
		}
	}
	public Money getCanTenderAmount(){
		Long canTender = this.amount.getCent()-this.tenderAmount.getCent();
		return new Money(canTender);
	}
	public int getTenderProgress(){
		return (int)(tenderAmount.getCent()*100/amount.getCent());
	}
	 
	public Long getLoanId() {
		return loanId;
	}
	public void setLoanId(Long loanId) {
		this.loanId = loanId;
	}
	
	public Money getAmount() {
		return amount;
	}
	public void setAmount(Money amount) {
		this.amount = amount;
	}
	public Money getRemainAmount() {
		return remainAmount;
	}
	public void setRemainAmount(Money remainAmount) {
		this.remainAmount = remainAmount;
	}
	public Money getReturnAmount() {
		return returnAmount;
	}
	public void setReturnAmount(Money returnAmount) {
		this.returnAmount = returnAmount;
	}
	public Integer getRepayNum() {
		return repayNum;
	}
	public void setRepayNum(Integer repayNum) {
		this.repayNum = repayNum;
	}

	public Money getTenderAmount() {
		return tenderAmount;
	}
	public void setTenderAmount(Money tenderAmount) {
		this.tenderAmount = tenderAmount;
	}
	public Money getFlowAmount() {
		return flowAmount;
	}
	public void setFlowAmount(Money flowAmount) {
		this.flowAmount = flowAmount;
	}
	public Money getRepoAmount() {
		return repoAmount;
	}
	public void setRepoAmount(Money repoAmount) {
		this.repoAmount = repoAmount;
	}
	public Date getFlowDate() {
		return flowDate;
	}
	public void setFlowDate(Date flowDate) {
		this.flowDate = flowDate;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Integer getOldStatus() {
		return oldStatus;
	}
	public void setOldStatus(Integer oldStatus) {
		this.oldStatus = oldStatus;
	}
	public Integer getTenderCount() {
		return tenderCount;
	}
	public void setTenderCount(Integer tenderCount) {
		this.tenderCount = tenderCount;
	}
	public Date getTenderBegin() {
		return tenderBegin;
	}
	public void setTenderBegin(Date tenderBegin) {
		this.tenderBegin = tenderBegin;
	}
	public Date getTenderEnd() {
		return tenderEnd;
	}
	public void setTenderEnd(Date tenderEnd) {
		this.tenderEnd = tenderEnd;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getExpiredTime() {
		return expiredTime;
	}

	public void setExpiredTime(Date expiredTime) {
		this.expiredTime = expiredTime;
	}

	public Money getTotalInterest() {
		return totalInterest;
	}

	public void setTotalInterest(Money totalInterest) {
		this.totalInterest = totalInterest;
	}
	
	public static void main(String[] args) {
		LoanVariation loanvariation = new LoanVariation();
		loanvariation.setTenderAmount(new Money(450,0));
		loanvariation.setAmount(new Money(900,0));
		System.out.println(loanvariation.getTenderProgress());
	}

}
